package Demo1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.SynchronousQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-14
 * Time: 13:58
 */
public class Test {
    //创建两个栈
    public Stack<Integer> stack1;
    public Stack<Integer> stack2;

    public Test() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if(empty()){
            return -1;
        }else{
            if(stack2.isEmpty() == false){
                return stack2.pop();
            }else{
                for(int i = 0;i < stack1.size();i++){
                    stack2.push(stack1.pop());
                }
                return stack2.pop();
            }
        }
    }

    public int peek() {
        if(empty()){
            return -1;
        }else{
            if(stack2.isEmpty() == false){
                return stack2.peek();
            }else{
                for(int i = 0;i < stack1.size();i++){
                    stack2.push(stack1.pop());
                }
                return stack2.peek();
            }
        }
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.push(1);
        t.push(2);
        int ret = 0;
        ret = t.peek();
        System.out.println(ret);
        ret = t.pop();
        System.out.println(ret);
        t.empty();
    }
    public static void main1(String[] args) {
//        Queue<Integer> queue = new Queue<Integer>();不能直接用来实例化对象，因为 Queue是接口
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        System.out.println(queue.peek());
        queue.poll();
        System.out.println(queue.peek());
        System.out.println(queue.isEmpty());

    }
}
